//
//  BKResultOfMianXueFeiViewController.m
//  baokaodaxue
//
//  Created by SKar on 15/6/2.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKResultOfMianXueFeiViewController.h"
#import "BKMianXueFeiDataSource.h"
#import "BKMianXueFeiTableViewCell.h"
#import "BKSegmentTextField.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import <MBProgressHUD.h>
#import "BKMianXueFeiViewModel.h"
@interface BKResultOfMianXueFeiViewController () <UITableViewDelegate>
@property (weak, nonatomic) IBOutlet BKSegmentTextField *pc;
@property (weak, nonatomic) IBOutlet BKSegmentTextField *dq;
@property (weak, nonatomic) IBOutlet BKSegmentTextField *lx;
@property (strong ,nonatomic) BKMianXueFeiViewModel *viewModel;
@property (nonatomic, strong) BKMianXueFeiDataSource *dataSource;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;

@end

static NSString * const cellID = @"MianXueFeiCell";

@implementation BKResultOfMianXueFeiViewController

- (instancetype)initWithNibByArray:(NSArray *)dataArray {
    self = [super initWithNibName:NSStringFromClass([BKResultOfMianXueFeiViewController class]) bundle:nil];
    if (self) {
       
    
        _dataSource = [[BKMianXueFeiDataSource alloc] init];
        _dataSource.dataArray = dataArray;
        _viewModel = [[BKMianXueFeiViewModel alloc] init];
    
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self confirmTableView];
    
    _headerView.layer.borderColor = [[UIColor colorWithRed:.5 green:.5 blue:.5 alpha:.5]CGColor];
    _headerView.layer.borderWidth = .5;
    
    @weakify(self);
    [[_pc rac_textSignal] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length] ==0) {
            return ;
        }
        [self performSelector:@selector(p_reloadData) withObject:nil];
    }];
    
    [[_dq rac_textSignal] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length] ==0) {
            return ;
        }
        [self performSelector:@selector(p_reloadData) withObject:nil];
    }];
    
    [[_lx rac_textSignal] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length] ==0) {
            return ;
        }
        [self performSelector:@selector(p_reloadData) withObject:nil];
    }];

}

- (void)dealloc {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
}

- (void)confirmTableView {
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BKMianXueFeiTableViewCell class]) bundle:nil] forCellReuseIdentifier:mianfei];
    self.tableView.dataSource = self.dataSource;
    self.tableView.delegate = self;
    [_tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSArray *array = _dataSource.dataArray[indexPath.row][@"major"];
    if ([array isKindOfClass:[NSArray class]]) {
        return [array count]>1?[array count]*44:44;
    }
    return 44;
    
}
- (void)p_reloadData{
    
    
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [hud show:YES];
    
    
    __weak BKResultOfMianXueFeiViewController *weakSelf = self;
    [_viewModel colleageWithPc:_pc.text dq:_dq.text lx:_lx.text success:^(NSArray *dataArray) {
        
        [hud hide:YES];
        [hud removeFromSuperview];
        weakSelf.dataSource.dataArray = dataArray;
        [weakSelf.tableView reloadData];
    } failure:^(NSString *message) {
        [hud hide:YES];
        [hud removeFromSuperview];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        
    }];
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 10;
    
}
@end
